package com.kust.orderService.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kust.commomUtils.JwtUtils;
import com.kust.commomUtils.Result;
import com.kust.orderService.entity.Order;
import com.kust.orderService.service.OrderService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-07-05
 */
@RestController
@RequestMapping("/orderService/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @ApiOperation("生成订单的方法")
    @PostMapping("createOrder/{courseId}")
    public Result saveOrder(@PathVariable("courseId") String courseId, HttpServletRequest request){
        //创建订单，返回订单号
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        String OrderNo = orderService.createOrders(courseId,memberIdByJwtToken);
        return Result.ok().data("OrderId",OrderNo);
    }

    @ApiOperation("根据订单id查询订单信息")
    @GetMapping("getOrderInfo/{orderId}")
    public Result getOrderInfo(@PathVariable("orderId") String orderId){
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",orderId);
        Order one = orderService.getOne(queryWrapper);
        return Result.ok().data("item",one);
    }

    @GetMapping("/isBuyCourse/{memberId}/{id}")
    public boolean isBuyCourse(@PathVariable("memberId") String memberId,
                               @PathVariable("id") String id) {
        if(memberId.isEmpty()){
            return false;
        }
        //订单状态是1表示支付成功
        int count = orderService.count(new QueryWrapper<Order>()
                .eq("member_id", memberId)
                .eq("course_id", id)
                .eq("status", 1));
        if(count>0) {
            return true;
        } else {
            return false;
        }
    }

}

